﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
///PicDao 的摘要说明
/// </summary>
public class PicDao
{
    DBMM dbm = new DBMM();
    DataTable dt = null;
    string sql = null;
    PicVo pv = new PicVo();

    //添加图片
    public Exception addPic(PicVo pv)
    {
        sql = "insert into Pic values(@Purl,@Id,@Ptype,@Pname)";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Purl", pv.Purl), 
                                                 new SqlParameter("@Id", pv.Id), 
                                                 new SqlParameter("@Ptype", pv.Ptype),
                                                 new SqlParameter("@Pname", pv.Pname)};
        Exception ex = null;
        ex = dbm.Execute(sql, sp);
        return ex;
    }

    //查找相关店面楼盘图片
    public DataTable getPic(string id, string type)
    {
        sql = "select top 8 * from Pic where Id=@Id and Ptype=@Ptype";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Id", id),
                                                 new SqlParameter("@Ptype", type)};
        dt = new DataTable();
        dt = dbm.GetTable(sql, sp);
        return dt;
    }

    //获得单个图片
    public PicVo getSinglePic(string id)
    {
        sql = "select * from Pic where Pid=@Pid";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Pid", id) };
        dt = new DataTable();
        dt = dbm.GetTable(sql, sp);
        if (dt.Rows.Count > 0)
        {
            pv = new PicVo();
            pv.Pid = Convert.ToInt32(dt.Rows[0][0]);
            pv.Purl = dt.Rows[0][1].ToString();
            pv.Id = Convert.ToInt32(dt.Rows[0][2]);
            pv.Ptype = Convert.ToInt32(dt.Rows[0][3]);
            pv.Pname = dt.Rows[0][4].ToString();
            return pv;
        }
        else
        {
            return null;
        }
    }

    //删除图片
    public Exception delPic(string id)
    {
        sql = "delete Pic where Pid=@Pid";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Pid", id) };
        Exception ex = null;
        ex = dbm.Execute(sql, sp);
        return ex;
    }

    //删除店面所有图片
    public Exception delShopPic(string id)
    {
        sql = "delete Pic where id=@id and Ptype=0";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@id", id) };
        Exception ex = null;
        ex = dbm.Execute(sql, sp);
        return ex;
    }

    //查找相关店面楼盘图片数量
    public int getPicNumber(string id, string type)
    {
        sql = "select count(*) from Pic where Id=@Id and Ptype=@Ptype";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Id", id),
                                                 new SqlParameter("@Ptype", type)};
        dt = new DataTable();
        dt = dbm.GetTable(sql, sp);
        return Convert.ToInt32(dt.Rows[0][0]);
    }
}
